home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 June / PCWorld_2007-06_cd.bin / v cisle / tclock / tclocklight-040702-3.exe / source / timer / dialog.c next >
C/C++ Source or Header  |  2004-09-07  |  15KB  |  546 lines

  1. /*-------------------------------------------------------------
  2.   dialog.c : Timer dialog
  3.   (C) Kazuto Sato 1997-2003
  4.   For the license, please read readme.txt.
  5.   
  6.   Written by Kazubon, Nanashi-san
  7. ---------------------------------------------------------------*/
  8.  
  9. #include "tctimer.h"
  10.  
  11. /* Globals */
  12.  
  13. void OnShowDialog(HWND hwnd);
  14.  
  15. HWND g_hDlg = NULL;
  16.  
  17. /* Statics */
  18.  
  19. BOOL CALLBACK DlgProcTimer(HWND, UINT, WPARAM, LPARAM);
  20. static void OnInit(HWND hDlg);
  21. static void OnDestroy(HWND hDlg);
  22. static void OnOK(HWND hDlg);
  23. static void OnCancel(HWND hDlg);
  24. static void OnHelp(HWND hDlg);
  25. static void OnName(HWND hDlg);
  26. static void OnNameDropDown(HWND hDlg);
  27. static void OnAdd(HWND hDlg);
  28. static void OnDelete(HWND hDlg);
  29. static void OnBrowse(HWND hDlg);
  30. static void OnTest(HWND hDlg);
  31. static void OnShowTime(HWND hDlg);
  32. static void OnUserStr(HWND hDlg);
  33. static void GetTimerFromDlg(HWND hDlg, PTIMERSTRUCT pTS);
  34. static void SetTimerToDlg(HWND hDlg, const PTIMERSTRUCT pTS);
  35. static void EnableTimerDlgItems(HWND hDlg);
  36.  
  37. static PTIMERSTRUCT m_pTimer = NULL;
  38. static int m_numTimer = 0;
  39. static int m_nCurrent = -1;
  40. static BOOL m_bPlaying = FALSE;  // sound is plaing
  41.  
  42. /*-------------------------------------------------------
  43.   TIMERM_SHOWDLG message
  44. ---------------------------------------------------------*/
  45. void OnShowDialog(HWND hwnd)
  46. {
  47.     if(g_hDlg && IsWindow(g_hDlg)) ;
  48.     else
  49.         g_hDlg = CreateDialog(g_hInst, MAKEINTRESOURCE(IDD_TIMER),
  50.             NULL, DlgProcTimer);
  51.     SetForegroundWindow98(g_hDlg);
  52. }
  53.  
  54. /*-------------------------------------------
  55.   dialog procedure
  56. ---------------------------------------------*/
  57. BOOL CALLBACK DlgProcTimer(HWND hDlg, UINT message,
  58.     WPARAM wParam, LPARAM lParam)
  59. {
  60.     switch(message)
  61.     {
  62.         case WM_INITDIALOG:
  63.             OnInit(hDlg);
  64.             return TRUE;
  65.         case WM_COMMAND:
  66.         {
  67.             int id, code;
  68.             id = LOWORD(wParam); code = HIWORD(wParam);
  69.             switch(id)
  70.             {
  71.                 case IDC_TIMERNAME:
  72.                     if(code == CBN_SELCHANGE)
  73.                         OnName(hDlg);
  74.                     else if(code == CBN_DROPDOWN)
  75.                         OnNameDropDown(hDlg);
  76.                     break;
  77.                 case IDC_TIMERADD:
  78.                     OnAdd(hDlg);
  79.                     break;
  80.                 case IDC_TIMERDEL:
  81.                     OnDelete(hDlg);
  82.                     break;
  83.                 case IDC_TIMERSANSHO:
  84.                     OnBrowse(hDlg);
  85.                     break;
  86.                 case IDC_TIMERTEST:
  87.                     OnTest(hDlg);
  88.                     break;
  89.                 case IDC_SHOWTIME:
  90.                     OnShowTime(hDlg);
  91.                     break;
  92.                 case IDC_SHOWWHOLE:
  93.                 case IDC_SHOWADD:
  94.                 case IDC_SHOWUSTR:
  95.                     OnUserStr(hDlg);
  96.                     break;
  97.                 case IDOK:
  98.                     OnOK(hDlg);
  99.                     break;
  100.                 case IDCANCEL:
  101.                     OnCancel(hDlg);
  102.                     break;
  103.                 case IDC_TIMERHELP:
  104.                     OnHelp(hDlg);
  105.                     break;
  106.             }
  107.             return TRUE;
  108.         }
  109.         // playing sound ended
  110.         case MM_MCINOTIFY:
  111.         case MM_WOM_DONE:
  112.             if(message == MM_MCINOTIFY)
  113.                 OnMCINotify(hDlg, wParam, (LONG)lParam);
  114.             else
  115.                 StopFile();
  116.             m_bPlaying = FALSE;
  117.             SendDlgItemMessage(hDlg, IDC_TIMERTEST,
  118.                 BM_SETIMAGE, IMAGE_ICON, (LPARAM)g_hIconPlay);
  119.             return TRUE;
  120.         case WM_DESTROY:
  121.             OnDestroy(hDlg);
  122.             break;
  123.     }
  124.     return FALSE;
  125. }
  126.  
  127. /*-------------------------------------------
  128.   initialize main dialog
  129. ---------------------------------------------*/
  130. void OnInit(HWND hDlg)
  131. {
  132.     HICON hIcon;
  133.     char section[20];
  134.     int i;
  135.     
  136.     // common/tclang.c
  137.     SetDialogLanguage(hDlg, "Timer", g_hfontDialog);
  138.     
  139.     hIcon = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_TCLOCK));
  140.     SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
  141.     
  142.     // ../common/dialog.c
  143.     SetMyDialgPos(hDlg, 32, 32);
  144.     
  145.     SendDlgItemMessage(hDlg, IDC_TIMERTEST, BM_SETIMAGE, IMAGE_ICON,
  146.         (LPARAM)g_hIconPlay);
  147.     
  148.     UpDown_SetBuddy(hDlg, IDC_TIMERSPIN1, IDC_TIMERMINUTE);
  149.     UpDown_SetBuddy(hDlg, IDC_TIMERSPIN2, IDC_TIMERSECOND);
  150.     
  151.     UpDown_SetRange(hDlg, IDC_TIMERSPIN1, 1440, 0);
  152.     UpDown_SetRange(hDlg, IDC_TIMERSPIN2, 59, 0);
  153.     
  154.     m_numTimer = GetMyRegLong(NULL, "TimerNum", 0);
  155.     if(m_numTimer > 0)
  156.     {
  157.         m_pTimer = (PTIMERSTRUCT)malloc(sizeof(TIMERSTRUCT) * m_numTimer);
  158.         for(i = 0; i < m_numTimer; i++)
  159.         {
  160.             PTIMERSTRUCT pTS = m_pTimer + i;
  161.             
  162.             wsprintf(section, "Timer%d", i + 1);
  163.             GetMyRegStr(section, "Name", pTS->name, BUFSIZE_NAME, section);
  164.             pTS->minute = GetMyRegLong(section, "Minute", 3);
  165.             pTS->second = GetMyRegLong(section, "Second", 0);
  166.             GetMyRegStr(section, "File", pTS->fname, MAX_PATH, "");
  167.             pTS->bRepeat = GetMyRegLong(section, "Repeat", FALSE);
  168.             pTS->bBlink = GetMyRegLong(section, "Blink", FALSE);
  169.             pTS->bDisp = GetMyRegLong(section, "Disp", FALSE);
  170.             pTS->nDispType = GetMyRegLong(section, "DispType", 1);
  171.             pTS->nUserStr = GetMyRegLong(section, "UserStr", 0);
  172.         }
  173.     }
  174.     else
  175.     {
  176.         m_numTimer = 1;
  177.         m_pTimer = (PTIMERSTRUCT)malloc(sizeof(TIMERSTRUCT));
  178.         memset(m_pTimer, 0, sizeof(TIMERSTRUCT));
  179.         strcpy(m_pTimer->name, "Timer1");
  180.         m_pTimer->minute = 3;
  181.         m_pTimer->nDispType = 1;
  182.     }
  183.     
  184.     for(i = 0; i < m_numTimer; i++)
  185.         CBAddString(hDlg, IDC_TIMERNAME, (LPARAM)(m_pTimer + i)->name);
  186.     
  187.     CBSetCurSel(hDlg, IDC_TIMERNAME, 0);
  188.     
  189.     OnName(hDlg);
  190. }
  191.  
  192. /*------------------------------------------------
  193.    clear up
  194. --------------------------------------------------*/
  195. void OnDestroy(HWND hDlg)
  196. {
  197.     g_hDlg = NULL;
  198.     
  199.     if(m_pTimer) free(m_pTimer);
  200.     m_pTimer = NULL;
  201.     m_numTimer = 0;
  202.     m_nCurrent = -1;
  203.     
  204.     StopFile();
  205.     m_bPlaying = FALSE;
  206. }
  207.  
  208. /*-------------------------------------------
  209.   "Start" button
  210. ---------------------------------------------*/
  211. void OnOK(HWND hDlg)
  212. {
  213.     char section[20];
  214.     int i, nOldTimer;
  215.     
  216.     /* save settings */
  217.     if(m_pTimer && (0 <= m_nCurrent && m_nCurrent < m_numTimer))
  218.         GetTimerFromDlg(hDlg, (m_pTimer + m_nCurrent));
  219.     
  220.     nOldTimer = GetMyRegLong(NULL, "TimerNum", 0);
  221.     if(nOldTimer < 1) nOldTimer = 0;
  222.     
  223.     SetMyRegLong(NULL, "TimerNum", m_numTimer);
  224.     
  225.     for(i = 0; i < m_numTimer && m_pTimer; i++)
  226.     {
  227.         PTIMERSTRUCT pTS = m_pTimer + i;
  228.         
  229.         wsprintf(section, "Timer%d", i + 1);
  230.         SetMyRegStr(section, "Name", pTS->name);
  231.         SetMyRegLong(section, "Minute", pTS->minute);
  232.         SetMyRegLong(section, "Second", pTS->second);
  233.         SetMyRegStr(section, "File", pTS->fname);
  234.         SetMyRegLong(section, "Repeat", pTS->bRepeat);
  235.         SetMyRegLong(section, "Blink", pTS->bBlink);
  236.         SetMyRegLong(section, "Disp", pTS->bDisp);
  237.         SetMyRegLong(section, "DispType", pTS->nDispType);
  238.         SetMyRegLong(section, "UserStr", pTS->nUserStr);
  239.     }
  240.     
  241.     for(i = m_numTimer; i < nOldTimer; i++)
  242.     {
  243.         wsprintf(section, "Timer%d", i + 1);
  244.         DelMyRegKey(section);
  245.     }
  246.     
  247.     /* start a timer */
  248.     if(m_pTimer && (0 <= m_nCurrent && m_nCurrent < m_numTimer))
  249.         TimerStart(m_pTimer + m_nCurrent);
  250.     
  251.     DestroyWindow(hDlg);
  252. }
  253.  
  254. /*-------------------------------------------
  255.   "Cancel" button
  256. ---------------------------------------------*/
  257. void OnCancel(HWND hDlg)
  258. {
  259.     if(!IsTimerRunning())
  260.         PostMessage(g_hwndTimer, WM_CLOSE, 0, 0);
  261.     DestroyWindow(hDlg);
  262. }
  263.  
  264. /*-------------------------------------------
  265.   "Help" button
  266. ---------------------------------------------*/
  267. void OnHelp(HWND hDlg)
  268. {
  269.     char helpurl[MAX_PATH], title[MAX_PATH];
  270.     
  271.     if(!g_langfile[0]) return;
  272.     
  273.     GetMyRegStr(NULL, "HelpURL", helpurl, MAX_PATH, "");
  274.     if(helpurl[0] == 0)
  275.     {
  276.         if(GetPrivateProfileString("Main", "HelpURL", "", helpurl,
  277.             MAX_PATH, g_langfile) == 0) return;
  278.     }
  279.     
  280.     if(GetPrivateProfileString("Timer", "HelpURL", "", title,
  281.         MAX_PATH, g_langfile) == 0) return;
  282.     
  283.     if(strlen(helpurl) > 0 && helpurl[ strlen(helpurl) - 1 ] != '/')
  284.         del_title(helpurl);
  285.     add_title(helpurl, title);
  286.     
  287.     ShellExecute(hDlg, NULL, helpurl, NULL, "", SW_SHOW);
  288. }
  289.  
  290. /*------------------------------------------------
  291.    "Name" is selected
  292. --------------------------------------------------*/
  293. void OnName(HWND hDlg)
  294. {
  295.     int index;
  296.     
  297.     if(!m_pTimer) return;
  298.     
  299.     if(0 <= m_nCurrent && m_nCurrent < m_numTimer)
  300.         GetTimerFromDlg(hDlg, m_pTimer + m_nCurrent);
  301.     
  302.     index = CBGetCurSel(hDlg, IDC_TIMERNAME);
  303.     if(0 <= index && index < m_numTimer)
  304.     {
  305.         SetTimerToDlg(hDlg, m_pTimer + index);
  306.         m_nCurrent = index;
  307.     }
  308. }
  309.  
  310. /*------------------------------------------------
  311.    combo box is about to be visible
  312.    set edited text to combo box
  313. --------------------------------------------------*/
  314. void OnNameDropDown(HWND hDlg)
  315. {
  316.     char name[BUFSIZE_NAME];
  317.     
  318.     if(!m_pTimer ||
  319.         !(0 <= m_nCurrent && m_nCurrent < m_numTimer)) return;
  320.     
  321.     GetDlgItemText(hDlg, IDC_TIMERNAME, name, BUFSIZE_NAME);
  322.     
  323.     if(strcmp(name, m_pTimer[m_nCurrent].name) != 0)
  324.     {
  325.         CBDeleteString(hDlg, IDC_TIMERNAME, m_nCurrent);
  326.         CBInsertString(hDlg, IDC_TIMERNAME, m_nCurrent, name);
  327.         strcpy(m_pTimer[m_nCurrent].name, name);
  328.     }
  329. }
  330.  
  331. /*------------------------------------------------
  332.   "Add" button
  333. --------------------------------------------------*/
  334. void OnAdd(HWND hDlg)
  335. {
  336.     PTIMERSTRUCT pTSNew;
  337.     TIMERSTRUCT ts;
  338.     
  339.     OnNameDropDown(hDlg);
  340.     
  341.     memset(&ts, 0, sizeof(TIMERSTRUCT));
  342.     wsprintf(ts.name, "Timer%d", m_numTimer+1);
  343.     ts.minute = 3;
  344.     ts.nDispType = 1;
  345.     
  346.     pTSNew = AddTimerStruct(m_pTimer, m_numTimer, &ts);
  347.     
  348.     m_nCurrent = m_numTimer;
  349.     CBAddString(hDlg, IDC_TIMERNAME, (LPARAM)ts.name);
  350.     CBSetCurSel(hDlg, IDC_TIMERNAME, m_numTimer);
  351.     SetTimerToDlg(hDlg, &ts);
  352.     
  353.     m_numTimer++;
  354.     if(m_pTimer) free(m_pTimer);
  355.     m_pTimer = pTSNew;
  356.     
  357.     if(m_numTimer == 1)
  358.         EnableTimerDlgItems(hDlg);
  359.     
  360.     PostMessage(hDlg, WM_NEXTDLGCTL, 1, FALSE);
  361. }
  362.  
  363. /*------------------------------------------------
  364.   delete an alarm
  365. --------------------------------------------------*/
  366. void OnDelete(HWND hDlg)
  367. {
  368.     PTIMERSTRUCT pTSNew;
  369.     
  370.     if(!m_pTimer || m_numTimer < 1) return;
  371.     
  372.     if(!(0 <= m_nCurrent && m_nCurrent < m_numTimer)) return;
  373.     
  374.     if(m_numTimer > 1)
  375.     {
  376.         pTSNew = DelTimerStruct(m_pTimer, m_numTimer, m_nCurrent);
  377.         
  378.         PostMessage(hDlg, WM_NEXTDLGCTL, 1, FALSE);
  379.         CBDeleteString(hDlg, IDC_TIMERNAME, m_nCurrent);
  380.         
  381.         if(m_nCurrent == m_numTimer - 1) m_nCurrent--;
  382.         CBSetCurSel(hDlg, IDC_TIMERNAME, m_nCurrent);
  383.         SetTimerToDlg(hDlg, (pTSNew + m_nCurrent));
  384.         
  385.         m_numTimer--;
  386.         if(m_pTimer) free(m_pTimer);
  387.         m_pTimer = pTSNew;
  388.     }
  389.     else
  390.     {
  391.         free(m_pTimer); m_pTimer = NULL;
  392.         m_numTimer = 0;
  393.         m_nCurrent = -1;
  394.         
  395.         CBDeleteString(hDlg, IDC_TIMERNAME, 0);
  396.         SetTimerToDlg(hDlg, NULL);
  397.         
  398.         EnableTimerDlgItems(hDlg);
  399.     }
  400. }
  401.  
  402. /*------------------------------------------------
  403.   "..." button - select file
  404. --------------------------------------------------*/
  405. void OnBrowse(HWND hDlg)
  406. {
  407.     char deffile[MAX_PATH], fname[MAX_PATH];
  408.     
  409.     GetDlgItemText(hDlg, IDC_TIMERFILE, deffile, MAX_PATH);
  410.     
  411.     // ../common/soundselect.c
  412.     if(!BrowseSoundFile(g_hInst, hDlg, deffile, fname))
  413.         return;
  414.     
  415.     SetDlgItemText(hDlg, IDC_TIMERFILE, fname);
  416.     PostMessage(hDlg, WM_NEXTDLGCTL, 1, FALSE);
  417. }
  418.  
  419. /*------------------------------------------------
  420.   test sound
  421. --------------------------------------------------*/
  422. void OnTest(HWND hDlg)
  423. {
  424.     char fname[MAX_PATH];
  425.     
  426.     GetDlgItemText(hDlg, IDC_TIMERFILE, fname, MAX_PATH);
  427.     if(fname[0] == 0) return;
  428.     
  429.     if((HICON)SendDlgItemMessage(hDlg, IDC_TIMERTEST,
  430.         BM_GETIMAGE, IMAGE_ICON, 0) == g_hIconPlay)
  431.     {
  432.         if(PlayFile(hDlg, fname, 0))
  433.         {
  434.             SendDlgItemMessage(hDlg, IDC_TIMERTEST, BM_SETIMAGE, IMAGE_ICON,
  435.                 (LPARAM)g_hIconStop);
  436.             InvalidateRect(GetDlgItem(hDlg, IDC_TIMERTEST), NULL, FALSE);
  437.             m_bPlaying = TRUE;
  438.         }
  439.     }
  440.     else
  441.     {
  442.         StopFile();
  443.         m_bPlaying = FALSE;
  444.     }
  445. }
  446.  
  447. /*------------------------------------------------
  448.   "Show remaining time" is checked
  449. --------------------------------------------------*/
  450. void OnShowTime(HWND hDlg)
  451. {
  452.     int i;
  453.     BOOL b;
  454.     
  455.     b = IsDlgButtonChecked(hDlg, IDC_SHOWTIME);
  456.     if(!m_pTimer) b = FALSE;
  457.     for(i = IDC_SHOWWHOLE; i <= IDC_SHOWUSTRNUM; i++)
  458.         EnableDlgItem(hDlg, i, b);
  459.     
  460.     OnUserStr(hDlg);
  461. }
  462.  
  463. /*------------------------------------------------
  464.   "User string" is checked
  465. --------------------------------------------------*/
  466. void OnUserStr(HWND hDlg)
  467. {
  468.     BOOL b;
  469.     b = IsDlgButtonChecked(hDlg, IDC_SHOWUSTR);
  470.     if(!IsDlgButtonChecked(hDlg, IDC_SHOWTIME)) b = FALSE;
  471.     if(!m_pTimer) b = FALSE;
  472.     
  473.     EnableDlgItem(hDlg, IDC_SHOWUSTRNUM, b);
  474. }
  475.  
  476. /*------------------------------------------------
  477.   get settings of a timer from the dialog
  478. --------------------------------------------------*/
  479. void GetTimerFromDlg(HWND hDlg, PTIMERSTRUCT pTS)
  480. {
  481.     if(!pTS) return;
  482.     
  483.     GetDlgItemText(hDlg, IDC_TIMERNAME, pTS->name, BUFSIZE_NAME);
  484.     pTS->minute = GetDlgItemInt(hDlg, IDC_TIMERMINUTE, NULL, FALSE);
  485.     pTS->second = GetDlgItemInt(hDlg, IDC_TIMERSECOND, NULL, FALSE);
  486.     GetDlgItemText(hDlg, IDC_TIMERFILE, pTS->fname, MAX_PATH);
  487.     pTS->bRepeat = IsDlgButtonChecked(hDlg, IDC_TIMERREPEAT);
  488.     pTS->bBlink = IsDlgButtonChecked(hDlg, IDC_TIMERBLINK);
  489.     pTS->bDisp  = IsDlgButtonChecked(hDlg, IDC_SHOWTIME);
  490.     if(IsDlgButtonChecked(hDlg, IDC_SHOWWHOLE))
  491.         pTS->nDispType  = 0;
  492.     else if(IsDlgButtonChecked(hDlg, IDC_SHOWUSTR))
  493.         pTS->nDispType  = 2;
  494.     else pTS->nDispType  = 1;
  495.     pTS->nUserStr = GetDlgItemInt(hDlg, IDC_SHOWUSTRNUM, NULL, FALSE);
  496. }
  497.  
  498. /*------------------------------------------------
  499.   set settings of a timer to the dialog
  500. --------------------------------------------------*/
  501. void SetTimerToDlg(HWND hDlg, const PTIMERSTRUCT pTS)
  502. {
  503.     SetDlgItemText(hDlg, IDC_TIMERNAME, pTS ? pTS->name : "");
  504.     SetDlgItemInt(hDlg,  IDC_TIMERMINUTE, pTS ? pTS->minute : 0, FALSE);
  505.     SetDlgItemInt(hDlg,  IDC_TIMERSECOND, pTS ? pTS->second : 0, FALSE);
  506.     SetDlgItemText(hDlg, IDC_TIMERFILE, pTS ? pTS->fname : "");
  507.     CheckDlgButton(hDlg, IDC_TIMERREPEAT, pTS ? pTS->bRepeat : FALSE);
  508.     CheckDlgButton(hDlg, IDC_TIMERBLINK, pTS ? pTS->bBlink : FALSE);
  509.     CheckDlgButton(hDlg, IDC_SHOWTIME, pTS ? pTS->bDisp : FALSE);
  510.     if(pTS)
  511.     {
  512.         if(pTS->nDispType == 0)
  513.             CheckRadioButton(hDlg, IDC_SHOWWHOLE, IDC_SHOWUSTR, IDC_SHOWWHOLE);
  514.         else if(pTS->nDispType == 2)
  515.             CheckRadioButton(hDlg, IDC_SHOWWHOLE, IDC_SHOWUSTR, IDC_SHOWUSTR);
  516.         else
  517.             CheckRadioButton(hDlg, IDC_SHOWWHOLE, IDC_SHOWUSTR, IDC_SHOWADD);
  518.     }
  519.     else CheckRadioButton(hDlg, IDC_SHOWWHOLE, IDC_SHOWUSTR, IDC_SHOWADD);
  520.     
  521.     SetDlgItemInt(hDlg,  IDC_SHOWUSTRNUM, pTS ? pTS->nUserStr : 1, FALSE);
  522.     
  523.     OnShowTime(hDlg);
  524. }
  525.  
  526. /*------------------------------------------------
  527.   enable/disable all dialog items
  528. --------------------------------------------------*/
  529. void EnableTimerDlgItems(HWND hDlg)
  530. {
  531.     HWND hwnd;
  532.     BOOL b = (m_pTimer != NULL);
  533.     
  534.     hwnd = GetWindow(hDlg, GW_CHILD);
  535.     while(hwnd)
  536.     {
  537.         if(GetDlgCtrlID(hwnd) == IDOK) break;
  538.         
  539.         if(GetDlgCtrlID(hwnd) != IDC_TIMERADD)
  540.             EnableWindow(hwnd, b);
  541.         
  542.         hwnd = GetWindow(hwnd, GW_HWNDNEXT);
  543.     }
  544. }
  545.  
  546.